<?php

namespace app\admin\controller;

use app\common\controller\admin\Base;
use think\Cache;
use think\Controller;
use think\Db;

class Index extends Controller
{


    protected $noNeedLogin = ['login','kefuLogin'];

    protected $model;


    public function _initialize()
    {
       
        parent::_initialize();
        $this->model = new \app\admin\model\Admin;
     
    }

    //登录
    public function login()
    {

        if ($this->request->isPost()) {
            $params = $this->request->post();
            //检测ip是否允许
            $login = $this->auth->login($params, 3600);
            if (!$login) {
                $this->error($this->auth->getError());
            }
            if (is_array($login)) {
                $this->success('已经登录了！', null, $login, 3);
            }
            $admin = $this->auth->getUserInfo($login);
            if ($admin) {
                $data['id'] = $admin['id'];
                $data['username'] = $admin['username'];
                $data['avatar'] = $admin['avatar'];
                $data['token'] = $admin['token'];
                $data['is_bind_google'] = $admin['is_bind_google'];
                $data['logintime'] = $admin['logintime'];
                $data['loginip'] = $admin['loginip'];
                $this->success('登录成功', null, $data);
            }
            $this->error('系统错误');
        }
    }

    //绑定谷歌验证器
    public function googleValidator()
    {
        if ($this->admin['is_bind_google'] == 1 && $this->admin['google_secret_key'] != '') {
            $this->error('您已经绑定过谷歌验证器了！');
        }
        if ($this->request->isPost()) {
            $params['google_code'] = $this->request->post('google_code');
            $params['secret'] = $this->admin['google_secret_key'];
            $data = $this->auth->checkGoogleCode($params);
            if ($data) {
                $this->model->update(['is_bind_google' => 1], ['id' => $this->admin['id']]);
                $this->auth->logout($this->admin['token']);
                $this->success('谷歌验证器绑定成功！');
            }
            $this->error('验证失败！');
        }
        if ($this->request->isGet()) {
            $data = $this->auth->getGoogleData();
            $this->model->update(['google_secret_key' => $data['secret']], ['id' => $this->admin['id']]);
            $admin = Cache::store('redis')->pull($this->admin['token']);
            $admin['google_secret_key'] = $data['secret'];
            Cache::store('redis')->set($this->admin['token'], $admin, $admin['token_expiration_time'] - time());
            $this->success('谷歌验证器信息', null, $data);
        }
    }

    //退出
    public function loginOut()
    {
        $out = $this->auth->logout($this->admin['token']);
        if ($out) {
            $this->success('退出成功');
        }
    }

    public function kefuLogin()
    {
        
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params['username'])) {
                $this->error('请输入客服名称');
            }
            if (empty($params['password'])) {
                $this->error('请输入客服密码');
            }
            $result = Db::table('su_kefu_user')->where('password', '=', md5("NRJ*@".$params['password']))->where('username', '=', $params['username'])->find();
            if (empty($result)) {
                $this->error('用户名密码错误');
            }
            $access_token = md5(uniqid() . mt_rand());
            $result['token'] = $access_token;
            $result['is_kefu'] = 1;
            $result['is_admin'] = 0;
            Cache::set($access_token, $result, 86400 * 100);
            $this->success("", "", $result);
        }
    }
}
